<?php

require "include/template_manager.inc.php";
if (substr($_REQUEST['titolo'], -1) == '/') {
    $_REQUEST['titolo'] =substr_replace($_REQUEST['titolo'], "", -1);
}
$query = "SELECT `v`.`Id` AS `id`, `v`.`Gradazione` AS `gradazione`, `v`.`Titolo` AS `titolo`, `v`.`Titolo_en` AS `titolo_en`,`v`.`Origine` AS `origine`,`v`.`uso` AS `uso`,`v`.`uso_en` AS `uso_en`,"
        . "`i`.`meta_description` as `meta_description`, `i`.`meta_description_en` as `meta_description_en`,`i`.`meta_keywords` as `meta_keywords`,`i`.`meta_keywords_en` as `meta_keywords_en`,"
        . "`v`.`Data` as `data`,`i`.`uri` as `uri`, concat(`p`.`nome`,' ',`p`.`cognome`) AS `autore`,"
        . "`p`.`titolo` AS `prenome`,`i`.`descrizione` AS `descrizione`,`i`.`descrizione_en` AS `descrizione_en` "
        . "FROM ((`vini` `v` join `immagine` `i`) join `professori` `p`) "
        . "WHERE (`v`.`immagine` = `i`.`id`) and (`v`.`autore` = `p`.`id`) and ";

if (getLanguage() == 'it') {
    $query .= "(`v`.`titolo` like '" . $_REQUEST['titolo'] . "')";
} else {
    $query .= "(`v`.`titolo_en` like '" . $_REQUEST['titolo'] . "')";
}

$prefix = "/";
$labelWine = "vino";
if (getLanguage() == 'en') {
    $prefix = "/en/";
    $labelWine = "wine";
}

if (($data = DB_Manager::getInstance()->query($query)) && ($row = $data->fetch_array(MYSQLI_ASSOC))) {
    require_once 'include/language_manager.inc.php';
    Language_Manager::getInstance();
    
    if (getLanguage() == 'it') {
        $titolo = $row['titolo'];
        $meta_description = $row['meta_description'];
        $meta_keywords = $row['meta_keywords'];
        $uso = $row['uso'];
        $descrizione = $row['descrizione'];
    } else {
        $titolo = $row['titolo_en'];
        $meta_description_en = $row['meta_description_en'];
        $meta_keywords_en = $row['meta_keywords_en'];
        $uso = $row['uso_en'];
        $descrizione = $row['descrizione_en'];
    }
    $meta["title"] = $titolo . "|" . L::meta_wine_title;
    $meta["description"] = $meta_description_en;
    $meta["keywords"] = $meta_keywords_en;
    $myTemplate = new Template_Manager("vino", null, true, 10, $meta);
    $myTemplate->setBodyContentString("id", $row['id']);
    $myTemplate->setBodyContentString("titolo", $titolo);
    $myTemplate->setBodyContentString("origine", $row['origine']);
    $myTemplate->setBodyContentString("uso", $uso);
    $myTemplate->setBodyContentString("autore", $row['autore']);
    $myTemplate->setBodyContentImage("immagine", $row['uri']);
    $myTemplate->setBodyContentString("gradazione", $row['gradazione']);
    $myTemplate->setBodyContentString("descr", $descrizione);
    $myTemplate->setCanonicalUrl($prefix . $labelWine . '/' . $_REQUEST['titolo'] . "/");
}

$myTemplate->setBodyContentString("nav_message", L::nav_message);
$myTemplate->setBodyContentString("nav_home", L::nav_home);
$myTemplate->setBodyContentString("nav_page", L::messages_wine_nav_page);

$myTemplate->setBodyContentString("description", L::messages_wine_description);
$myTemplate->setBodyContentString("origin", L::messages_wine_origin);
$myTemplate->setBodyContentString("grad", L::messages_wine_graduation);